package com.iterator.config;

import com.iterator.bean.web.lyh.utils.BaseRespVO;
import com.iterator.bean.web.pojo.BaseRespVo;
import com.iterator.bean.wechat.dhy.ResultVO;
import com.iterator.mapper.web.aop.AopMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: Silence_W
 * @date: 2021-01-14 21:31
 **/
@Component
@Aspect
public class IteratorAop {

    @Autowired
    AopMapper aopMapper;

    @Pointcut("execution( * com.iterator.controller.web..*(..))")
    public void Login(){

    }

    @Around("Login()")
    public Object doAfter(ProceedingJoinPoint joinPoint) throws Throwable {

        Subject subject = SecurityUtils.getSubject();
        String  username = (String) subject.getPrincipal();
        Session session = subject.getSession();
        String host = session.getHost();
        String name = joinPoint.getSignature().getName();
        int status= 1;

        Object  proceed = joinPoint.proceed();
        if (proceed instanceof com.iterator.bean.web.pojo.BaseRespVo){
            com.iterator.bean.web.pojo.BaseRespVo p= (BaseRespVo) proceed;
            int errno = (int) ((BaseRespVo) proceed).getErrno();
            if (errno==0){
                status=1;

            } else {
                status=0;
            }
        }
        else if(proceed instanceof com.iterator.bean.web.stone.BaseRespVo){
            com.iterator.bean.web.stone.BaseRespVo p= (com.iterator.bean.web.stone.BaseRespVo) proceed;
            int errno = (int) ((com.iterator.bean.web.stone.BaseRespVo) proceed).getErrno();
            if (errno==0){
                status=1;

            } else {
                status=0;
            }

        }
        else if(proceed instanceof BaseRespVO){
            BaseRespVO p= (BaseRespVO) proceed;
            if (p.getErrno()==(long) 0){

                status =1;
            }else {
                status=0;
            }
        } else if (proceed instanceof ResultVO){
            ResultVO result= (ResultVO) proceed;
            int errno = result.getErrno();
            if (errno==0){
                status=1;

            }  else {          status=0;}
        }

        String result=null;
        if (status==1){
            result="成功";
        }else if(status==0){
            result="失败";
        }

        aopMapper.insertLog(username,host,1,name,status,result,null,new Date(),new Date(),0);

        return proceed;
    }
}
